package n92_任务调度线程池;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author fu
 * @date 2022-04-05 14:23
 * 概要：
 * ---------------------------------------------------------------
 * |                                                              |
 * |                  |
 * |                                                             |
 * |=============================================================
 */
public class T4_应用_任务调度_指定每周几点运行 {

    public static void main(String[] args) {
        // 每周四 18:00 定时执行任务

        // 一、period : 一周的间隔时间
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>HEAD
//        long period = 1000*60*60*24*7;
// ==================================================
        long period = 5000;
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<END

        // 二、initailDelay : 现在 和 未来周四的间隔毫秒
        // 1. 当前时间和周四 的时间差
        LocalDateTime now      = LocalDateTime.now();// 当前时间
System.out.println(now);
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>HEAD
//        LocalDateTime thursDay = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
// ==================================================
        LocalDateTime thursDay = now.withHour(15).withMinute(16).withSecond(0).withNano(0).with(DayOfWeek.TUESDAY);
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<END

        // 2. 这个周四必须是未来的时间
        if (now.compareTo(thursDay)>0) {
            thursDay = thursDay.plusWeeks(1);
        }
        // 3. 计算现在 和 未来周四的间隔毫秒
        long initailDelay = Duration.between(now, thursDay).toMillis();
        System.out.println(initailDelay);

        System.out.println(thursDay);



        // 三、延迟 和 循环
        final ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);

        pool.scheduleAtFixedRate(()->{
            System.out.println("running......");
        },initailDelay, period, TimeUnit.MILLISECONDS);

    }
}
